package optiondialoges;

import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.BorderLayout;
import javax.swing.JDialog;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Dimension;
import javax.swing.JCheckBox;

import panel.GlyphPanel;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

public class SurfacePlotOptionDialog extends JDialog implements ActionListener,
		WindowListener {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JPanel buttonPanel = null;
	private JButton okButton = null;
	private JButton cancelButton = null;
	private JPanel optionPanel = null;
	private JLabel jLabel = null;
	private JTextField multiplTextField = null;
	private JCheckBox drawWfCheckBox = null;
	private JCheckBox shadeCheckBox = null;
	private JCheckBox axisCheckBox = null;
	private JCheckBox srcbackgrCheckBox = null;
	private JCheckBox fillPlotbackgCheckBox = null;
	private JCheckBox onePolCheckBox = null;
	private JCheckBox smoothCheckBox = null;
	private String multiplOldValue;
	private boolean draw, shade, axis, srcbckg, fill, onePol, smooth;
	private GlyphPanel panel;

	/**
	 * @param owner
	 */
	public SurfacePlotOptionDialog(Frame owner, GlyphPanel panel) {
		super(owner);
		this.panel=panel;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(303, 321);
		this.setContentPane(getJContentPane());
		this.addWindowListener(this);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getButtonPanel(), BorderLayout.SOUTH);
			jContentPane.add(getOptionPanel(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes buttonPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getButtonPanel() {
		if (buttonPanel == null) {
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.insets = new Insets(0, 10, 20, 10);
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.insets = new Insets(0, 10, 20, 10);
			buttonPanel = new JPanel();
			buttonPanel.setLayout(new GridBagLayout());
			buttonPanel.add(getOkButton(), gridBagConstraints1);
			buttonPanel.add(getCancelButton(), gridBagConstraints);
		}
		return buttonPanel;
	}

	/**
	 * This method initializes okButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getOkButton() {
		if (okButton == null) {
			okButton = new JButton();
			okButton.setText("Ok");
			okButton.addActionListener(this);
		}
		return okButton;
	}

	/**
	 * This method initializes cancelButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getCancelButton() {
		if (cancelButton == null) {
			cancelButton = new JButton();
			cancelButton.setText("Cancel");
			cancelButton.addActionListener(this);
		}
		return cancelButton;
	}

	/**
	 * This method initializes optionPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getOptionPanel() {
		if (optionPanel == null) {
			GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
			gridBagConstraints10.insets = new Insets(0, 0, 20, 0);
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.gridx = 0;
			gridBagConstraints9.anchor = GridBagConstraints.WEST;
			gridBagConstraints9.insets = new Insets(0, 10, 0, 0);
			gridBagConstraints9.gridy = 7;
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.gridx = 0;
			gridBagConstraints8.anchor = GridBagConstraints.WEST;
			gridBagConstraints8.insets = new Insets(0, 10, 0, 0);
			gridBagConstraints8.gridy = 6;
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.gridx = 0;
			gridBagConstraints7.anchor = GridBagConstraints.WEST;
			gridBagConstraints7.insets = new Insets(0, 10, 0, 0);
			gridBagConstraints7.gridy = 5;
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.gridx = 0;
			gridBagConstraints6.anchor = GridBagConstraints.WEST;
			gridBagConstraints6.insets = new Insets(0, 10, 0, 0);
			gridBagConstraints6.gridy = 4;
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.gridx = 0;
			gridBagConstraints5.anchor = GridBagConstraints.WEST;
			gridBagConstraints5.insets = new Insets(0, 10, 0, 0);
			gridBagConstraints5.gridy = 3;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 0;
			gridBagConstraints4.anchor = GridBagConstraints.WEST;
			gridBagConstraints4.insets = new Insets(0, 10, 0, 0);
			gridBagConstraints4.gridy = 2;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 0;
			gridBagConstraints3.anchor = GridBagConstraints.WEST;
			gridBagConstraints3.insets = new Insets(0, 10, 0, 0);
			gridBagConstraints3.gridy = 1;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints2.gridy = 0;
			gridBagConstraints2.weightx = 1.0;
			gridBagConstraints2.insets = new Insets(0, 0, 20, 0);
			gridBagConstraints2.gridx = 1;
			jLabel = new JLabel();
			jLabel.setText("Polygon multiplier(10-200%)");
			optionPanel = new JPanel();
			optionPanel.setLayout(new GridBagLayout());
			optionPanel.add(jLabel, gridBagConstraints10);
			optionPanel.add(getMultiplTextField(), gridBagConstraints2);
			optionPanel.add(getDrawWfCheckBox(), gridBagConstraints3);
			optionPanel.add(getShadeCheckBox(), gridBagConstraints4);
			optionPanel.add(getAxisCheckBox(), gridBagConstraints5);
			optionPanel.add(getSrcbackgrCheckBox(), gridBagConstraints6);
			optionPanel.add(getFillPlotbackgCheckBox(), gridBagConstraints7);
			optionPanel.add(getOnePolCheckBox(), gridBagConstraints8);
			optionPanel.add(getSmoothCheckBox(), gridBagConstraints9);
		}
		return optionPanel;
	}

	/**
	 * This method initializes multiplTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getMultiplTextField() {
		if (multiplTextField == null) {
			multiplTextField = new JTextField();
			multiplTextField.setPreferredSize(new Dimension(60, 21));
			multiplTextField.addPropertyChangeListener("value",
					new PropertyChangeListener() {

						@Override
						public void propertyChange(PropertyChangeEvent evt) {
							multiplOldValue = (String) evt.getOldValue();

						}
					});
		}
		return multiplTextField;
	}

	/**
	 * This method initializes drawWfCheckBox
	 * 
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getDrawWfCheckBox() {
		if (drawWfCheckBox == null) {
			drawWfCheckBox = new JCheckBox();
			drawWfCheckBox.setText("Draw Wireframe");
			drawWfCheckBox.setName("Draw Wireframe");
		}
		return drawWfCheckBox;
	}

	/**
	 * This method initializes shadeCheckBox
	 * 
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getShadeCheckBox() {
		if (shadeCheckBox == null) {
			shadeCheckBox = new JCheckBox();
			shadeCheckBox.setText("Shade");
			shadeCheckBox.setSelected(true);
			shadeCheckBox.setName("Shade");
		}
		return shadeCheckBox;
	}

	/**
	 * This method initializes axisCheckBox
	 * 
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getAxisCheckBox() {
		if (axisCheckBox == null) {
			axisCheckBox = new JCheckBox();
			axisCheckBox.setText("Draw axis");
			axisCheckBox.setSelected(true);
			axisCheckBox.setName("Draw axis");
		}
		return axisCheckBox;
	}

	/**
	 * This method initializes srcbackgrCheckBox
	 * 
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getSrcbackgrCheckBox() {
		if (srcbackgrCheckBox == null) {
			srcbackgrCheckBox = new JCheckBox();
			srcbackgrCheckBox.setText("Source background is lighter");
			srcbackgrCheckBox.setName("Source background is lighter");
			srcbackgrCheckBox.setMnemonic(KeyEvent.VK_UNDEFINED);
		}
		return srcbackgrCheckBox;
	}

	/**
	 * This method initializes fillPlotbackgCheckBox
	 * 
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getFillPlotbackgCheckBox() {
		if (fillPlotbackgCheckBox == null) {
			fillPlotbackgCheckBox = new JCheckBox();
			fillPlotbackgCheckBox.setText("Fill plot background with black");
			fillPlotbackgCheckBox.setName("Fill plot background with black");
		}
		return fillPlotbackgCheckBox;
	}

	/**
	 * This method initializes onePolCheckBox
	 * 
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getOnePolCheckBox() {
		if (onePolCheckBox == null) {
			onePolCheckBox = new JCheckBox();
			onePolCheckBox.setText("One Polygon per Line");
			onePolCheckBox.setName("One Polygon per Line");
		}
		return onePolCheckBox;
	}

	/**
	 * This method initializes smoothCheckBox
	 * 
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getSmoothCheckBox() {
		if (smoothCheckBox == null) {
			smoothCheckBox = new JCheckBox();
			smoothCheckBox.setText("Smooth");
			smoothCheckBox.setSelected(true);
		}
		return smoothCheckBox;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		JButton button = (JButton) e.getSource();
		if (button.getText().contentEquals("Ok")) {
			multiplOldValue = multiplTextField.getText();
			draw = drawWfCheckBox.isSelected();
			shade = shadeCheckBox.isSelected();
			axis = axisCheckBox.isSelected();
			srcbckg = srcbackgrCheckBox.isSelected();
			fill = fillPlotbackgCheckBox.isSelected();
			onePol = onePolCheckBox.isSelected();
			smooth = smoothCheckBox.isSelected();
			panel.setDialog(this);
			panel.setOptionsSet(true);
			String command=String.format("run(\"Surface Plot...\", \"polygon=%s",multiplTextField.getText());
			if(draw)command=String.format("%s draw_wireframe", command);
			if(shade)command=String.format("%s shade", command);
			if(axis)command=String.format("%s draw_axis", command);
			if(srcbckg)command=String.format("%s source", command);
			if(fill)command=String.format("%s fill", command);
			if(onePol)command=String.format("%s one", command);
			if(smooth)command=String.format("%s smooth", command);
			command=String.format("%s\");", command);
			
			panel.setCommand(command);

		} else if (button.getText().contentEquals("Cancel")) {
			multiplTextField.setText(multiplOldValue);
			drawWfCheckBox.setSelected(draw);
			shadeCheckBox.setSelected(shade);
			axisCheckBox.setSelected(axis);
			srcbackgrCheckBox.setSelected(srcbckg);
			fillPlotbackgCheckBox.setSelected(fill);
			onePolCheckBox.setSelected(onePol);
			smoothCheckBox.setSelected(smooth);
		}
		this.setVisible(false);
	}

	@Override
	public void windowActivated(WindowEvent e) {
		

	}

	@Override
	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosing(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowOpened(WindowEvent e) {
		multiplOldValue = multiplTextField.getText();
		draw = drawWfCheckBox.isSelected();
		shade = shadeCheckBox.isSelected();
		axis = axisCheckBox.isSelected();
		srcbckg = srcbackgrCheckBox.isSelected();
		fill = fillPlotbackgCheckBox.isSelected();
		onePol = onePolCheckBox.isSelected();
		smooth = smoothCheckBox.isSelected();
	}

} // @jve:decl-index=0:visual-constraint="10,10"
